﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Jinjia.Approve.Common.Enum;
using Jinjia.Approve.Entity;
using Jinjia.Approve.Entity.Entity.JJFlow;
using Jinjia.Approve.IRepository.JJFlow;
using Jinjia.Approve.Model.VM.JJFlow;
using Jinjia.Core.BaseInfrastructure.StorageModel;
using Jinjia.Core.BaseInfrastructure.StorageModel.Impl;
using Jinjia.Core.UnitOfWork.UnitOfWorkComponent;
using SqlSugar;

namespace Jinjia.Approve.Repository.JJFlow
{
    public class FlowTrackRepository : BaseRepository<jj_flow_track>, IFlowTrackRepository
    {
        public FlowTrackRepository(IUnitOfWork unitOfWork, IInitUserInfo initUserInfo) : base(unitOfWork, initUserInfo, "")
        {
        }

        public async Task<int> Save(jj_flow_track data)
        {
            if (data.id <= 0) //新增
            {
                var id = await InsertAsync(data);
                return id;
            }
            else
            {
                await UpdateAsync(data);
                return data.id;
            }
        }

        public async Task Save(List<jj_flow_track> data)
        {
            var insertData = data.Where(x => x.id <= 0).ToList();

            if (insertData.Any())
            {
                await _db.Insertable(insertData).ExecuteCommandAsync();
            }

            var updateData = data.Where(x => x.id > 0).ToList();

            if (updateData.Any())
            {
                await _db.Updateable(insertData).ExecuteCommandAsync();
            }
        }

        public async Task<bool> GetApproveTrackModelByInstanceId(int instance_id)
        {
            //var track_model = await _db.Queryable<jj_flow_track>()
            //    .Where(x => x.instance_id == instance_id && x.operation_result != (int)EnumDb.JJFlowOperationTypeEnum.Start)
            //    .FirstAsync();

            return await _db.Queryable<jj_flow_instance>()
                .LeftJoin<jj_node_reference>((a, b) => a.current_node_id == b.node_id)
               .Where((a, b) => a.id == instance_id && b.sequence > 0)
               .AnyAsync(); ;
        }

        public async Task<List<JjFlowTrackVM>> GetListByBizOrderID(string biz_order_id, string modular_key, int flow_instance_id)
        {
            var list = await _db.Queryable<jj_flow_instance>()
                .Where(x => x.biz_order_id == biz_order_id && x.status == "10" && x.modular_key == modular_key && x.id != flow_instance_id)
                .ToListAsync();
            var track_list = new List<JjFlowTrackVM>();
            if (list != null && list.Count > 0)
            {
                var instance_ids = list.Select(x => x.id).ToList();
                track_list = await _db.Queryable<jj_flow_track, erpa_user_tbl>((a, b) => new JoinQueryInfos(
                        JoinType.Left, a.create_user_id == b.id))
                    .LeftJoin<erpa_user_tbl>((a, b, c) => c.id.ToString() == a.from_emp)
                    .In(a => a.instance_id, instance_ids)
                    .OrderBy(a => a.create_time)
                    .Select<JjFlowTrackVM>("a.*,b.user_name as emp_name,c.user_name as from_emp_name, 0 as node_status_type,a.create_time op_time")
                    .ToListAsync();
            }

            return track_list;
        }

        public async Task<List<jj_flow_track>> GetFlowTracks(int instance_id)
        {
            var list = await _db.Queryable<jj_flow_track, erpa_user_tbl, jj_flow_instance, jj_node, erpa_user_tbl>((a, b, c, d, e) =>
                    new JoinQueryInfos(
                        JoinType.Left, a.create_user_id == b.id,
                        JoinType.Left, a.instance_id == c.id,
                        JoinType.Left, a.node_id == d.id,
                        JoinType.Left, a.from_emp == e.id.ToString()
                    ))
                .Where(a => a.instance_id == instance_id)
                .Select("a.*,a.create_time,b.user_name as emp_name,e.user_name as from_emp_name,if(a.node_id <> c.current_node_id,0,1) as node_status_type,d.emp_type")
                .OrderBy(" a.create_time asc")
                .ToListAsync();
            return list;
        }

        public async Task<List<jj_flow_track>> GetListByInstanceId(int instance_id)
        {
            return await _db.Queryable<jj_flow_track>()
                .Where(x => x.instance_id == instance_id)
                .ToListAsync();
        }
    }
}